package com.mars.btrace.demo;

import com.sun.btrace.AnyType;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.*;

/**
 * author by: mars
 * Date: 2019/5/25 22:56
 * Description: 拦截异常
 */
@BTrace
public class BtraceDemo5 {

    @TLS
    static Throwable throwable;

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = "<init>"
    )
    public static void onthrow(@Self Throwable self){ // new Throwable()
        throwable = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = "<init>"
    )
    public static void onthrow1(@Self Throwable self,String s){ // new Throwable(String msg)
        throwable = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = "<init>"
    )
    public static void onthrow2(@Self Throwable self,String s,Throwable cause){ // new Throwable(String msg, Throwable cause)
        throwable = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = "<init>"
    )
    public static void onthrow3(@Self Throwable self,Throwable cause){ // new Throwable( Throwable cause)
        throwable = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = "<init>",
            location = @Location(Kind.RETURN)
    )
    public static void onthrowReturn(){
        if(throwable !=null) {
            BTraceUtils.Threads.jstack(throwable);
            BTraceUtils.println("===============");
            throwable = null;
        }
    }
}
